<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?:'main';
if($_RQ['op']=='main'){
	//这里整理跳转到游戏页面
	$id=intval($_RQ['id']);
	$game=pdo_get('jy_weishop_game',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($game['type']!=4){
		model('jy_weishop/game')->check_game(0,$id);
	}
	$game['pluginset']=$game['pluginset']?json_decode($game['pluginset'],true):array('rewardtype'=>0);
	if($game['merchid']){
		$sender=pdo_get('jy_merch_user',array('id'=>$game['merchid'],'uniacid'=>$_SESSION['uniacid']), array('merchname','logo','desc'));
		$sender['nickname']=$sender['merchname'];
		$sender['avatar']=$sender['logo'];
	}elseif($game['mid']){
		$sender=pdo_get('core_members',array('id'=>$game['mid'],'uniacid'=>$_SESSION['uniacid']), array('nickname', 'avatar'));
	}else{
		$sender=array(
			'nickname'=>$_SESSION['account']['title'],
			'avatar'=>$_SESSION['account']['logo'],
		);
	}
	//获取红包领取记录
	$iscanyu_num=pdo_count('jy_weishop_game_feelog',array('game_id'=>$game['id'],'isshare'=>0,'uniacid'=>$_SESSION['uniacid']));
	$allgetfee=pdo_getsum('jy_weishop_game_feelog',array('game_id'=>$game['id'],'uniacid'=>$_SESSION['uniacid']),'fee');
    $myfeelog=pdo_get('jy_weishop_game_feelog',array('game_id'=>$game['id'],'pin_id'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
    $pinfee=pdo_getsum('jy_weishop_game_feelog',array('game_id'=>$game['id'],'pin_id'=>$myfeelog['id'],'uniacid'=>$_SESSION['uniacid']),'fee');
	$isfeered=pdo_getcolumn('jy_weishop_game_feelog',array('game_id'=>$game['id'],'isshare'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
	$isgetred=pdo_getcolumn('jy_weishop_game_getlog',array('game_id'=>$game['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
	$cangetred=$myfee>=$game['pluginset']['red_man']?1:0;
	$red = array(
			'isfeered'=>$isfeered,
			'isgetred'=>$isgetred,
			'cangetred'=>$cangetred,
			'red_man' => $game['pluginset']['red_man'],
            'isideal' => 'yes',
            'result' => 'success',
            'total_fee' => $game['pluginset']['total_fee'],
            'allgetfee' => $allgetfee,
            'myfee' => $myfeelog['fee'] + $pinfee,
			'pinfee' => $pinfee,
            'total' => $game['pluginset']['total'],
            'get_count' => $iscanyu_num,
			'canshare'=>$game['pluginset']['total']>$iscanyu_num,
            'title' => $game['title'],
    );
	$return['typename']=$this->rewardtype[$game['pluginset']['rewardtype']];
	$return['game']=$game;
	$return['sender']=$sender;
	$return['red']=$red;
	$return['list']=array();
	if($_RQ['pin_id']){
		$this->session('pin_id',$_RQ['pin_id']);
	}
	$return['sharedata']=array(
		'title'=> '来领红包啦！！！',
		'desc'=> $game['title'],
		'link'=>custom_url('jy_red_pin/game//id='.$id.'&pin_id='.$myfeelog['id'],true),
		'imgUrl'=> tomedia('../addons/jy_red_pin/images/red_rain.png'),
	);
	exi($return);
}
if($_RQ['op']=='getred'){
	$post=get_postdata();	
	$id=intval($post['id']);
	$res=model('jy_weishop/game')->get_gameres(0,$id);
	exi($res);
}